package com.nexus.all.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nexus.all.dto.CommentDTO;
import com.nexus.all.dto.CommentQueryDTO;
import com.nexus.all.enums.NoteLikeTypeEnum;
import com.nexus.all.manager.CommentManager;
import com.nexus.all.manager.NoteLikeManager;
import com.nexus.all.manager.UserManager;
import com.nexus.all.model.Comment;
import com.nexus.all.model.NoteLike;
import com.nexus.all.model.User;
import com.nexus.all.service.CommentService;
import com.nexus.all.vo.CommentVO;
import com.nexus.bean.query.QueryResultVO;
import com.nexus.bean.utils.BeanUtils;
import com.nexus.bean.utils.CollectionsUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Service
@AllArgsConstructor
public class CommentServiceImpl implements CommentService {

    private final CommentManager commentManager;
    private final UserManager userManager;
    private final NoteLikeManager noteLikeManager;

    @Override
    public void addComment(CommentDTO dto) {

        if (Objects.isNull(dto.getParentId())) {
            dto.setParentId(0L);
        }

        Comment comment = BeanUtils.beanCopy(dto, Comment.class);
        commentManager.save(comment);
    }

    @Override
    public QueryResultVO<CommentVO> queryComment(CommentQueryDTO dto) {
        //默认查一级
        if (Objects.isNull(dto.getParentId())) {
            dto.setParentId(0L);
        }
        Page<Comment> page = commentManager.queryComment(dto);
        QueryResultVO<CommentVO> resultVO = BeanUtils.pageToQueryResultVO(page, CommentVO.class);

        List<CommentVO> records = BeanUtils.beanCopy(page.getRecords(), CommentVO.class);
        if (CollectionsUtil.isEmpty(records)) {
            resultVO.setRecords(new ArrayList<>());
            return resultVO;
        }

        List<Long> userIds = records.stream().map(CommentVO::getUserId).distinct().collect(Collectors.toList());
        List<User> userList = userManager.listByIds(userIds);
        Map<Long, User> userMap = userList.stream().collect(Collectors.toMap(User::getId, Function.identity()));

        Map<Long, NoteLike> noteLikeMap = new HashMap<>();
        if (Objects.nonNull(dto.getUserId())) {
            //登录，需要查是否点赞
            List<Long> commentIds = records.stream().map(CommentVO::getId).collect(Collectors.toList());
            List<NoteLike> noteLikeList = noteLikeManager.isLiked(commentIds, NoteLikeTypeEnum.COMMENT.getValue(), dto.getUserId());
            noteLikeMap = noteLikeList.stream().collect(Collectors.toMap(NoteLike::getLikeDataId, Function.identity()));
        }

        for (CommentVO commentVO : records) {
            //设置用户昵称
            User user = userMap.get(commentVO.getUserId());
            commentVO.setNickName(user.getNickName());
            commentVO.setAvatarUrl(user.getAvatarUrl());

            //如果没有评论和点赞 默认为0
            if (Objects.isNull(commentVO.getCommentCount())) {
                commentVO.setCommentCount(0);
            }
            if (Objects.isNull(commentVO.getLikeCount())) {
                commentVO.setLikeCount(0);
            }

            //是否点赞过
            if (Objects.nonNull(noteLikeMap.get(commentVO.getId()))) {
                commentVO.setIsLiked(Boolean.TRUE);
            }
        }

        resultVO.setRecords(records);
        return resultVO;
    }
}
